<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-3-8
 * Time: 下午4:29
 */

namespace Home\Controller;

use Common\Controller\CommonController;
use Common\Api\SendPhoneCodeApi;

class ModifyMemberController extends CommonController {

   
    /**
     * ajax手机验证
     */
    function ajaxCheckPhone($phone) {
        $phone = urldecode($phone);
        $data = array();
        if(!checkMobile($phone)){
            $data['msg'] = "手机号不正确！";
            $data['status'] = 0;
        }else{
            $M_member = M('Member');
            $where['phone']  = $phone;
            $r = $M_member->field('member_id')->where($where)->find();
            if($r){
                $data['msg'] = "此手机已经绑定过！请更换手机号";
                $data['status'] = 0;
            }else{
                $data['msg'] = "";
                $data['status'] = 1;
            }
        }
        $this->ajaxReturn($data);
    }

    /**
     * ajax验证手机验证码
     */
    public function ajaxSandPhone(){
    	//echo 2;die;
        $phone =I('phone');
        //dump($phone);die;
        if(empty($phone)){
            $data['status']=0;
            $data['info'] = "参数错误";
            $this->ajaxReturn($data,'JSONP');
        }
        if(!preg_match("/^1[34578]{1}\d{9}$/",$phone)){  
            $data['status']=-1;
            $data['info'] = "手机号码不正确";
            $this->ajaxReturn($data,'JSONP');
        }  
        $user_phone=M("Member")->field('phone')->where("phone='$phone'")->find();
//         if (!empty($user_phone)){
//             $data['status']=-2;
//             $data['info'] = "手机号码已经存在";
//             $this->ajaxReturn($data);
//         }
	 	/* $ip = get_ip();
		$v = S('ip_phone'.$ip);
		$sss = S('limit_phone_ip');
		if($v > 10){
			$sss[$ip] = 1;
			S('limit_phone_ip',$sss);
		}
		if($sss[$ip] == 1){
			$data['status'] = -8;
			$data['info'] = '此IP已经被禁止发送短信';
			$this->ajaxReturn($data);
		} */
		$where['key']="CODE_NAME";
		$config=M('Config')->where($where)->find()['value'];
		$a=new SendPhoneCodeApi($phone);
        //$r = new SendPhoneCodeApi($phone);
    	//echo 1; die;
        $d=$a->send();
 
        if(!empty($d)){
			
			/*  $time = S('ip_phone_time'.$ip);
			if( $time == null || time() - $time > 60 ){
				S('ip_phone_time'.$ip,time());
				S('ip_phone'.$ip,1);
			}else{
				++$v;
				S('ip_phone'.$ip,$v);
				S('ip_phone_time'.$ip,time());
			}  */
			
            $data['status']=1;
            $data['info'] = "发送成功";
            $this->ajaxReturn($data,'JSONP');
        }else{
        	
            $data['status']=0;
            $data['info'] = "发送失败";
            $this->ajaxReturn($data,'JSONP');
        }
    }
}